<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Upload;
use Think\Image;
use Think\Page;
class GoodsController extends BaseController{
	public function add(){
		$model=D('Category');
		$data=$model->where('cid>0')->select();
		$this->assign('data',$data);
		$this->display('add');
	}
	public function addok(){
		if(isset($_POST['submit'])){
			
			$model=D('Goods');
	//图片文件上传------------------------------------------------------------------------------------------
			if($data=$model->create()){
				if($_FILES['photo']['error']==0){
					//设置保存路径
					$config=array(
	//注意注意：./是对于index.php的目录下的路径.......注意...........注意..............注意...............注意..........注意........
				       'rootPath'=>'./application/Public/Uploads/'		
					);
					$upload=new Upload($config);
					//var_dump($upload);
					//开始上传
					if($info=$upload->uploadOne($_FILES['photo'])){
						$data['photo']=$info["savepath"].$info["savename"];									
					}
  //============================================================================
					//exit;
			     //生成缩略图===================================================================
			     $image=new Image();
			     //打开原图
			     $photo=$upload->rootPath.$data['photo'];
			     $image->open($photo);
			     //生成缩略图
			     $image->thumb(40,40);
			     //保存缩略图
			     $small_img=$upload->rootPath.$info["savepath"].'small_'.$info["savename"];//相当于先给缩略图起个名字
			     $image->save($small_img);//保存
			     $data['small_photo']=$info["savepath"].'small_'.$info["savename"];		
				}		
			//文件上传结束---------------------------------------------------------------------------------
			//下面判断价格
			if(empty($_POST['price'])){
				$data['price']=0;
			}
			//$data=$model->create(); 创建
			//$data['name']=I('post.name');
			//$data['price']=I('post.price');
			//$data['photo']=I('post.photo');
			//$data['remark']=I('post.remark');
			if($model->add($data)){
				$this->redirect('admin',array(),2,'添加成功...');
			}else{
				$this->error('添加失败',U('add'),2);
				exit;
			}
		}
	  }
	}
	//显示管理页面----------------------这里需要分页的-----------------------------------
	public function admin(){
			$p=empty($_GET['p'])?1:$_GET['p'];
			$this->assign('p',$p);//-----------p----------p---------p-------p---------p---p-----	
		//$list=D('Category');
       	$model=D('Goods');
  /*$data=$model->relation(true)->select();
		echo "<pre>";
		var_dump($data);
		exit;*/
		//$model=new \Model\GoodsModel();如果model不在模块文件下面，实例化的是基础模型；在的话就是实例化子类模型
		//分页+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		$totalRows=$model->count();//总记录数
		$page=new Page($totalRows,8);//实例化
       
		      //更改分页样式++++++++++++++++++++++++++
		$page->lastSuffix=false;//不显示最后页码
		$page->rollPage=5;//每页显示5个页码
		$page->setConfig('first', '首页');
		$page->setConfig('pre','上一页');
		$page->setConfig('next', '下一页');
		$page->setConfig('last', '最后一页');
		$page->setConfig('current', '当前页');
		$page->setConfig('theme', '共%TOTAL_ROW% 条记录,当前是%NOW_PAGE%/%TOTAL_PAGE%  %FIRST%%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
		      //更改结束++++++++++++++++++++++++++++++++
		$show=$page->show();//分配页码
		$data=$model->relation(true)->order('id desc')->limit($page->firstRow.",".$page->listRows)->select();//分页数据	
		//分页结束++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		//$data['p']=$_GET['p'];
		//$data=$model->select();
		//$this->ajaxReturn($data);
        $this->assign('show',$show);//分配进去+++++++使用正好藏+++
		$this->assign('data',$data);
		$this->display('admin');
	}
	//修改-------------------------------------------------------------
	public function edit(){
		if(!empty($_GET['p'])){
			$p=$_GET['p'];  //-----------p----------p---------p-------p---------p---p-----	
			$this->assign('p',$p);
		}
		$id=$_GET['id'];
		$data=D('Category')->select();	
		$row=D('Goods')->find($id);
     	$this->assign('row',$row);
		$this->assign('data',$data);
		$this->display('edit');
	}
	public function editok(){
		if(isset($_POST['submit'])){
			$model=D('Goods');
			$p=$_POST['p'];	//-----------p----------p---------p-------p---------p---p-----	
		   $data=$model->create();
                  // $data['id']=$_POST['id'];
		//判断下价格
		if(empty($_POST['price'])){
			$data['price']=0;
		}
			//修改图片================上传文件====================================================================
			if($_FILES['photo']['error']==0){
				$config=array(
						'rootPath'=>'./application/Public/Uploads/',
				);
				$upload=new Upload($config);//new完了后，下面就是开始上传并且获取上传的图片信息
				if($info=$upload->uploadOne($_FILES['photo'])){
					$data['photo']=$info['savepath'].$info['savename'];
				}
			
			//缩略图--------------------------------------------------------------------
			$image=new Image();
			    //打开原图
			    $photo=$upload->rootPath.$data['photo'];
			    $image->open($photo);
			    //生成缩略图
			    $image->thumb(40,40);
			    //保存:先找出保存缩略图的路径，再保存；最后一步就是获取缩略图的信息
			    $small_img=$upload->rootPath.$info["savepath"].'small_'.$info['savename'];
			    $image->save($small_img);
			    $data['small_photo']=$info['savepath'].'small_'.$info['savename'];
			   }	 
				//else{$data['photo']=I('post.old_photo');}
		//------------------------------------------------------------------------
			//修改图片结束====================注意下面一行==========不全等于：因为类型不等===================================================
			if($model->save($data)!==false){//注意注意注意加上了!==false这个语句就是>=0条影响记录执行修改成功跳转
	
				$this->redirect("admin",array('p'=>$p),2,'修改成功...');
				//$this->success('修改成功',U('admin'),2);
			}else{
				$this->error('修改失败',U('edit'),2);
				exit;
			}  
		}
	} 
	//删除----------------------------------------------------------------------
	public function deleteAll(){
		if(isset($_POST['deleteSubmit'])){
			$goods=D ('Goods');
			$id=$_POST['id'];
			for($i=0;$i<count($id);$i++){
				$row=$goods->find($id[$i]);
				//判断图片是否有值++++++++++++++++++++++++++
				if($row['photo']){
					//判断是否删除
					if(!unlink('./application/Public/Uploads/'.$row['photo'])){
						echo "删除失败:./application/Public/Uploads/".$row['photo'];
						exit();
					}
				}
				
				$goods->delete($id[$i]);
			}
			
			$this->success('删除成功',U('admin'),2);
		}
	}
	//查询-------------------------------------------------------------+++++++++++++++++++++++
	public function select(){
		$goods=D('goods');
		$list=$goods->select();
		$this->display('select');
	}
	public function createselect(){
		$goods=D('goods');
		$list=$goods->select();
		$this->ajaxReturn($list);
	}
	public function selectok(){
		$id=$_POST['id'];
		$goods=D ('goods');
		$data=$goods->relation(true)->where("id='$id'")->find();
	    $this->ajaxReturn($data);
	}
	public function createfirst(){
		$cid=$_POST['id'];
		$model=new \Admin\Model\CategoryModel();  //这样直接new模型和D()就解决了商品类别的问题，但是放在别的控制器中就不行了;
		$data=$model->where("id='$cid'")->select();
		$this->ajaxReturn($data);
	}
}